﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;

/*
 * Author:Conis
 * Date:
 * Description:执行SQL
 */


namespace Conis.Squirrel.SqlBuilder.SQLite
{
    /// <summary>
    /// 执行SQL
    /// </summary>
    public class ExecuteSql : BaseExecuteSql
    {
        public ExecuteSql(SQLiteConnection connection, SQLiteTransaction transaction) : base(connection, transaction) { }


        /// <summary>
        /// 获取一个Command对象
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="commandType"></param>
        /// <param name="paramArray"></param>
        /// <returns></returns>
        protected override IDbCommand GetCommand(string sql, CommandType commandType, params IDataParameter[] paramArray)
        {
            SQLiteCommand cmd = new SQLiteCommand(sql, this.Connection as SQLiteConnection);
            cmd.CommandType = commandType;
            if (this.Transaction != null)
            {
                cmd.Transaction = this.Transaction as SQLiteTransaction;
            }
            if (paramArray != null)
            {
                cmd.Parameters.AddRange(paramArray);
            }
            return cmd;
        }

        /// <summary>
        /// 填充数据到DataTable
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        protected override DataTable FillToTable(IDbCommand cmd)
        {
            using (SQLiteDataAdapter adp = new SQLiteDataAdapter(cmd as SQLiteCommand))
            {
                DataTable dt = new DataTable();
                adp.Fill(dt);
                adp.Dispose();
                return dt;
            }

        }
    }
}
